package pid

// PID 控制器结构体
type PID struct {
	Kp, Ki, Kd float64
	prevError  float64
	integral   float64
}

// PID 控制器计算方法
func (pid *PID) Compute(setpoint, measured float64, dt float64) float64 {
	error := setpoint - measured
	p := pid.Kp * error
	pid.integral += error * dt
	i := pid.Ki * pid.integral
	d := pid.Kd * (error - pid.prevError) / dt
	pid.prevError = error
	return p + i + d
}
